namespace ScrumSprintMonitor.Samples.Feed
{
    #region #using Directives

    using System;
    using System.ComponentModel.Composition;

    using Extensibility;
    using Extensibility.Events;

    using Infrastructure;

    using Microsoft.Practices.Composite.Events;

    #endregion

    [Export(typeof(ScrumSprintMonitorAddIn))]
    [AddIn(Name = "Feed")]
    public class FeedAddIn : ScrumSprintMonitorAddIn
    {
        #region Private Fields

        private FeedBuilderService feedBuilderService;

        #endregion

        #region Overrides of ScrumSprintMonitorAddIn

        public override void Load()
        {
            var eventAggregator = ServiceLocator.GetInstance<IEventAggregator>();
            eventAggregator.GetEvent<MonitorStartedEvent>().Subscribe(OnMonitorStarted);
        }

        private void OnMonitorStarted(EventArgs obj)
        {
            var reportableData = ServiceLocator.GetInstance<IReportableData>();
            var teamMemberInfoProvider = ServiceLocator.GetInstance<ITeamMemberInfoProvider>();
            var logger = ServiceLocator.GetInstance<ILogger<FeedBuilderService>>();
            var configurationManager = ServiceLocator.GetInstance<IConfigurationManager>();

            this.feedBuilderService = new FeedBuilderService(ServiceLocator.GetInstance<IAddInHost<FeedAddIn>>(), reportableData,
                                                             teamMemberInfoProvider, configurationManager, logger, ServiceLocator);
        }

        public override void Unload()
        {
            if (this.feedBuilderService != null)
            {
                this.feedBuilderService.Dispose();
                this.feedBuilderService = null;
            }
        }

        #endregion
    }
}